package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.HrmsHolidayDictionaryMapper;
import com.example.demo.pojo.HrmsHolidayDictionary;
import com.example.demo.service.HrmsHolidayDictionaryService;
import com.example.demo.utils.RedisConst;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

/**
 * @author wangyunfei-jwk
 * @date 2024-02-04 17:12
 * @desciption:
 */
@Slf4j
@Service
public class HrmsHolidayDictionaryServiceImpl extends ServiceImpl<HrmsHolidayDictionaryMapper, HrmsHolidayDictionary> implements HrmsHolidayDictionaryService {
    @Resource
    private HrmsHolidayDictionaryService dictionaryService;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private RedissonClient redissonClient;
    @Override
    public List<HrmsHolidayDictionary> getAll() {
        StopWatch sp = new StopWatch();
        sp.start("============");
        ValueOperations ops = redisTemplate.opsForValue();
        List<HrmsHolidayDictionary> dictionaries = (List<HrmsHolidayDictionary>) ops.get(RedisConst.SKU_KEY_LIST);
        if (CollectionUtils.isNotEmpty(dictionaries)) {
            sp.stop();
            log.info("redis 用时：" + sp.getTotalTimeMillis() + "毫秒");//用时：375毫秒
            return dictionaries;
        }
        List<HrmsHolidayDictionary> list = dictionaryService.list();
        ops.set(RedisConst.SKU_KEY_LIST,list);
        sp.stop();
        log.info("mysql 用时：" + sp.getTotalTimeMillis() + "毫秒");//用时：375毫秒
        return list;
    }

    @Override
    public HrmsHolidayDictionary getByHrmsId(String param) {
        Long id = Long.valueOf(param);
        StopWatch sp = new StopWatch();
        sp.start("============");
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConst.SKU_BLOOM_FILTER);
        if (!bloomFilter.contains(id)) {
            log.info("访问id不存在:{},请再次尝试一次，范围1-30", id);
            bloomFilter.add(id);
            throw new RuntimeException("访问id不存在，请再次尝试一次，范围1-30");
        }
        List<HrmsHolidayDictionary> dictionaries = (List<HrmsHolidayDictionary>) redisTemplate.opsForValue().get(RedisConst.SKU_KEY_LIST);
        if (CollectionUtils.isEmpty(dictionaries)){
            CompletableFuture.runAsync(() -> {
                try {
                    List<HrmsHolidayDictionary> list = dictionaryService.list();
                    redisTemplate.opsForValue().set(RedisConst.SKU_KEY_LIST,list);
                } catch (Exception e) {
                    log.info("异步存储Redis 异常");
                }
            });
            return dictionaryService.getById(param);
        }
        for (HrmsHolidayDictionary dictionary : dictionaries) {
            if (Objects.equals(dictionary.getId(), id)){
                sp.stop();
                log.info("Redis 用时：" + sp.getTotalTimeMillis() + "毫秒");//用时：375毫秒
                return dictionary;
            }
        }
        sp.stop();
        log.info("redis 用时：" + sp.getTotalTimeMillis() + "毫秒");//用时：375毫秒
        return null;
    }
}
